/**
* Author: fulishang
* Create Time  : 2016年3月20日,上午12:41:34
* Modify Time :
* Desc  : 
*/
package com.samfu.poi;

import java.io.FileOutputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelWriter {

	private static XSSFWorkbook workBook;

	private static void insertData(XSSFSheet sheet) {
		for (int i=1; i<=10; i++) {
			
			XSSFRow row = sheet.createRow(i);
			
			XSSFCell name = row.createCell(0);
	        name.setCellType(XSSFCell.CELL_TYPE_STRING);  
	        XSSFRichTextString nameContent = new XSSFRichTextString("Sam " + i);
	        name.setCellValue(nameContent);
	        
	        XSSFCell address = row.createCell(1);
	        address.setCellType(XSSFCell.CELL_TYPE_STRING);
	        XSSFRichTextString addressContent = new XSSFRichTextString("TianTai " + i);
	        address.setCellValue(addressContent);
		}
	}
	
	private static void writeToExcel(String filePath) throws Exception {

        workBook = new XSSFWorkbook();  
        XSSFSheet sheet = workBook.createSheet();  
       
        //create the sheet header
        XSSFRow row = sheet.createRow(0);  
        XSSFCell code = row.createCell(0);  
        code.setCellType(XSSFCell.CELL_TYPE_STRING);  
        XSSFRichTextString codeContent = new XSSFRichTextString("Name");  
        code.setCellValue(codeContent);  
        XSSFCell city = row.createCell(1);  
        city.setCellType(XSSFCell.CELL_TYPE_STRING);  
        XSSFRichTextString cityContent = new XSSFRichTextString("Address");  
        city.setCellValue(cityContent);  
        
        //insert data
        insertData(sheet);
        
        FileOutputStream fos = new FileOutputStream(filePath);  
        workBook.write(fos);  
        fos.flush();  
        fos.close();  
        System.out.println("Excel file generated...");  
		
	}
	
	public static void main(String[] args) throws Exception{
		ExcelWriter.writeToExcel("/Users/fulishang/development/workspace/POIDemo/test1.xlsx");
	}
}
